$(document).ready(function () {

    /*- logo -*/
    $('.logo').sliderkit({
        auto: true,
        autospeed: 2500,
        tabs: true,
        freeheight: true,
        circular: true,
        panelfx: "none"
    });
    /*- promo-slider -*/
    var owl = $(".promo-slider");
    owl.owlCarousel({
        autoPlay: 5000,
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false
    });
    $('#stop-owl').mousemove(function () {
        owl.trigger('owl.stop');
    });
    /*- promo-slider -*/
    $(".hardware-repair__description__slider").owlCarousel({
        autoPlay: false,
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false
    });
    /*- slider-models -*/
    $(".random-models-slide").owlCarousel({
        autoPlay: false,
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 6,
        itemsDesktop: [1024, 5],
        itemsDesktopSmall: [960, 4],
        itemsTablet: [760, 3],
        itemsMobile: [580, 2],
    });

    $(".slider-models2").owlCarousel({
        autoPlay: false,
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 5,
        itemsDesktop: [1024, 5],
        itemsDesktopSmall: [960, 4],
        itemsTablet: [760, 3],
        itemsMobile: [580, 2],
    });

    $(".slider-models-ato-play").owlCarousel({
        autoPlay: true,
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 6,
        itemsDesktop: [1024, 5],
        itemsDesktopSmall: [960, 4],
        itemsTablet: [760, 3],
        itemsMobile: [580, 2],
    });
    /*- tabs -*/
    var galleryBig = $(".tabs__content");
    galleryBig.owlCarousel({
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false,
        autoHeight: true,
        mouseDrag: false,
        touchDrag: false,
        afterMove: function () {
            var slider = gallerySmall.data('owlCarousel');
            slider.userItems.removeClass('active').eq(this.currentSlide).addClass('active');
            slider.goTo(this.currentSlide);
        }
    });
    var gallerySmall = $(".tabs__title");
    gallerySmall.owlCarousel({
        items: 5,
        itemsDesktop: [1280, 4],
        itemsDesktopSmall: [1024, 3],
        itemsTablet: [820, 2],
        itemsMobile: [580, 1],
        afterInit: function () {
            this.userItems.eq(0).addClass('active');
            this.userItems.find('a').on('click', galleryBigUpdate);
        },
        afterMove: function () {
            $.each(this.userItems, function (index, value) {
                if (index == 0) {
                    if (value.className == "item active") {
                        $('#phones').show();
                        $('#noutbook').hide();
                    }
                }
                if (index == 1) {
                    if (value.className == "item active") {
                        $('#phones').hide();
                        $('#noutbook').show();
                    }
                }
                if (index == 2) {
                    if (value.className == "item active") {
                        $('#phones').hide();
                        $('#noutbook').hide();
                    }
                }
            });
        }
    });
    function galleryBigUpdate() {
        var slider = galleryBig.data('owlCarousel');
        var current = $(this).parents('.owl-item').index();
        slider.goTo(current);
        return false;
    }

    $(".next").click(function () {
        gallerySmall.trigger('owl.next');
        galleryBig.trigger('owl.next');
    });
    $(".prev").click(function () {
        gallerySmall.trigger('owl.prev');
        galleryBig.trigger('owl.prev');
    });
    /*- global-form__tabs -*/
    $(".global-form__tabs").sliderkit({
        auto: false,
        tabs: true,
        freeheight: true,
        circular: true,
        panelfx: "none"
    });
    /*- social-network__tabs -*/
    $(".social-network__tabs").sliderkit({
        auto: false,
        tabs: true,
        freeheight: true,
        circular: true,
        panelfx: "none"
    });
    /*- main-navi__button -*/
    $('.main-navi__button').click(function (e) {

        if ($(this).parent().hasClass('open')) {
            $(this).parent().removeClass('open');
        } else {
            $(this).parent().addClass('open');
        }
    });
    /*- hardware-repair__info -*/
    $('.hardware-repair__name__link').click(function (e)
    {
//		$('li.hardware-repair__info__item').removeClass("open");
//		if ($(this).parents('.hardware-repair__info__item').hasClass('open')) {
//			$(this).parents('.hardware-repair__info__item').removeClass('open');
//		} else {
//			$(this).parents('.hardware-repair__info__item').addClass('open');
//		}
        if ($(this).parent('li').parent('ul').parent('.hardware-repair__info__item').is('.open')) {
            $(this).parent('li').parent('ul').parent('.hardware-repair__info__item').removeClass('open');
            $(this).parent('li').parent('ul').parent('.hardware-repair__info__item').removeClass('search');
        } else {
            $('li.hardware-repair__info__item').removeClass("open");
            $('li.hardware-repair__info__item').removeClass("search");
            $(this).parent('li').parent('ul').parent('.hardware-repair__info__item').addClass('open');
            $(this).parent('li').parent('ul').parent('.hardware-repair__info__item').addClass('search');
        }
    });
    /*- feedback-list__form -*/
//	$('.feedback-list__form__link').click(function(e) {
//		
//		if($(this).parent().hasClass('open')) {
//			$(this).parent().removeClass('open');
//		} else {
//			$(this).parent().addClass('open');
//		}
//	});	

    /*- hardware-repair__close -*/
    $('.hardware-repair__close').click(function (e) {

        if ($(this).parent().hasClass('open')) {
            $(this).parent().removeClass('open');
        }
    });
    /*- header__top-panel__navi__section -*/
    $('.header__top-panel__navi__section').click(function (e) {

        if ($(this).parent().hasClass('open')) {
            $(this).parent().removeClass('open');
        } else {
            $(this).parent().addClass('open');
        }
    });
    /*- rate -*/
    // $('.rate').raty({ score: 4 });

    /*- choice-courier -*/
    $('.choice-courier').on('click', '.checkbox label', function (e) {
        if ($(this).parent().hasClass('open')) {
            $(this).parent().removeClass('open');
        } else {
            $(this).parent().addClass('open');
        }
    });
    /*- user-panel__button -*/
    $('.user-panel__button').click(function (e) {

        if ($(this).parent().hasClass('open')) {
            $(this).parent().removeClass('open');
        } else {
            $(this).parent().addClass('open');
        }
    });
    /*- search-form__button -*/
    $('.search-form__button').click(function (e) {

        if ($(this).parent().hasClass('open')) {
            $(this).parent().removeClass('open');
        } else {
            $(this).parent().addClass('open');
        }
    });
    /*- global-tabs -*/
    tabsBig = $(".global-tabs__content");
    tabsBig.owlCarousel({
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false,
        mouseDrag: window.location.pathname == "/dlya-postavshikov" ? false : true,
        autoHeight: true,
        afterMove: function ()
        {
            if (window.location.pathname == "/dlya-postavshikov")
            {
                var lang = "ru";

                if (this.currentSlide == 1)
                {
                    lang = "en";
                }
                else if (this.currentSlide == 2)
                {
                    lang = "ch";
                }

                $("#suppliers input").each(function (key)
                {
                    var data = $(this).data(lang);
                    $(this).attr('placeholder', data);
                });

                $("#suppliers textarea").each(function (key)
                {
                    var data = $(this).data(lang);
                    $(this).attr('placeholder', data);
                });

                $('#supplier-h3').text($('#supplier-h3').data(lang));
                $('#supplier-label').text($('#supplier-label').data(lang));
                $('#supplier-span').text($('#supplier-span').data(lang));
                $('input[type="submit"][name="yt2"]').attr("value", $('input[type="submit"][name="yt2"]').data(lang));
            }

            var slider = tabsSmall.data('owlCarousel');
            slider.userItems.removeClass('active').eq(this.currentSlide).addClass('active');
            slider.goTo(this.currentSlide);
        }
    });

    var tabsSmall = $(".global-tabs__title");
    tabsSmall.owlCarousel({
        items: 3,
        itemsDesktop: [1380, 2],
        itemsDesktopSmall: [1023, 1],
        itemsTablet: [768, 2],
        itemsMobile: [680, 1],
        afterInit: function () {
            this.userItems.eq(0).addClass('active');
            this.userItems.find('a').on('click', tabsBigUpdate);
        }
    });
    function tabsBigUpdate() {
        var slider = tabsBig.data('owlCarousel');
        var current = $(this).parents('.owl-item').index();
        slider.goTo(current);
        return false;
    }

    $(".next").click(function ()
    {
        tabsSmall.trigger('owl.next');
    });
    $(".prev").click(function ()
    {
        tabsSmall.trigger('owl.prev');
    });
    /*- photo-gallery -*/
    var gallerBigs = $("#photo-gallery .photo-gallery__big");
    gallerBigs.owlCarousel({
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false,
        afterMove: function () {
            var slider = gallerSmalls.data('owlCarousel');
            slider.userItems.removeClass('active').eq(this.currentSlide).addClass('active');
            slider.goTo(this.currentSlide);
        }
    });
    var gallerSmalls = $("#photo-gallery .photo-gallery__small");
    gallerSmalls.owlCarousel({
        items: 4,
        itemsDesktop: [920, 3],
        itemsDesktopSmall: [768, 5],
        itemsTablet: [768, 4],
        itemsMobile: [460, 2],
        afterInit: function () {
            this.userItems.eq(0).addClass('active');
            this.userItems.find('a').on('click', gallerBigsUpdate);
        }
    });
    function gallerBigsUpdate()
    {
        var slider = gallerBigs.data('owlCarousel');
        var current = $(this).parents('.owl-item').index();
        slider.goTo(current);
        return false;
    }

    $("#photo-gallery .next").click(function ()
    {
        gallerSmalls.trigger('owl.next');
    });
    $("#photo-gallery .prev").click(function ()
    {
        gallerSmalls.trigger('owl.prev');
    });

    /*- photo-gallery2 -*/
    var gallerBig2 = $("#photo-gallery2 .photo-gallery__big");
    gallerBig2.owlCarousel({
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false,
        afterMove: function () {
            var slider = gallerSmall2.data('owlCarousel');
            slider.userItems.removeClass('active').eq(this.currentSlide).addClass('active');
            slider.goTo(this.currentSlide);
        }
    });
    var gallerSmall2 = $("#photo-gallery2 .photo-gallery__small");
    gallerSmall2.owlCarousel({
        items: 4,
        itemsDesktop: [920, 3],
        itemsDesktopSmall: [768, 5],
        itemsTablet: [768, 4],
        itemsMobile: [460, 2],
        afterInit: function () {
            this.userItems.eq(0).addClass('active');
            this.userItems.find('a').on('click', gallerBig2Update);
        }
    });
    function gallerBig2Update()
    {
        var slider = gallerBig2.data('owlCarousel');
        var current = $(this).parents('.owl-item').index();
        slider.goTo(current);
        return false;
    }

    $("#photo-gallery2 .next").click(function ()
    {
        gallerSmall2.trigger('owl.next');
    });
    $("#photo-gallery2 .prev").click(function ()
    {
        gallerSmall2.trigger('owl.prev');
    });

    /*- photo-gallery3 -*/
    var gallerBig3 = $("#photo-gallery3 .photo-gallery__big");
    gallerBig3.owlCarousel({
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false,
        afterMove: function () {
            var slider = gallerSmall3.data('owlCarousel');
            slider.userItems.removeClass('active').eq(this.currentSlide).addClass('active');
            slider.goTo(this.currentSlide);
        }
    });
    var gallerSmall3 = $("#photo-gallery3 .photo-gallery__small");
    gallerSmall3.owlCarousel({
        items: 4,
        itemsDesktop: [920, 3],
        itemsDesktopSmall: [768, 5],
        itemsTablet: [768, 4],
        itemsMobile: [460, 2],
        afterInit: function () {
            this.userItems.eq(0).addClass('active');
            this.userItems.find('a').on('click', gallerBig3Update);
        }
    });
    function gallerBig3Update()
    {
        var slider = gallerBig3.data('owlCarousel');
        var current = $(this).parents('.owl-item').index();
        slider.goTo(current);
        return false;
    }

    $("#photo-gallery3 .next").click(function ()
    {
        gallerSmall3.trigger('owl.next');
    });
    $("#photo-gallery3 .prev").click(function ()
    {
        gallerSmall3.trigger('owl.prev');
    });

    /*- photo-gallery4 -*/
    var gallerBig4 = $("#photo-gallery4 .photo-gallery__big");
    gallerBig4.owlCarousel({
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false,
        afterMove: function () {
            var slider = gallerSmall4.data('owlCarousel');
            slider.userItems.removeClass('active').eq(this.currentSlide).addClass('active');
            slider.goTo(this.currentSlide);
        }
    });
    var gallerSmall4 = $("#photo-gallery4 .photo-gallery__small");
    gallerSmall4.owlCarousel({
        items: 4,
        itemsDesktop: [920, 3],
        itemsDesktopSmall: [768, 5],
        itemsTablet: [768, 4],
        itemsMobile: [460, 2],
        afterInit: function () {
            this.userItems.eq(0).addClass('active');
            this.userItems.find('a').on('click', gallerBig4Update);
        }
    });
    function gallerBig4Update()
    {
        var slider = gallerBig4.data('owlCarousel');
        var current = $(this).parents('.owl-item').index();
        slider.goTo(current);
        return false;
    }

    $("#photo-gallery4 .next").click(function ()
    {
        gallerSmall4.trigger('owl.next');
    });
    $("#photo-gallery4 .prev").click(function ()
    {
        gallerSmall4.trigger('owl.prev');
    });

    /*- photo-gallery5 -*/
    var gallerBig5 = $("#photo-gallery5 .photo-gallery__big");
    gallerBig5.owlCarousel({
        navigation: true, // Show next and prev buttons
        slideSpeed: 300,
        paginationSpeed: 400,
        items: 1,
        itemsDesktop: false,
        itemsDesktopSmall: false,
        itemsTablet: false,
        itemsMobile: false,
        afterMove: function () {
            var slider = gallerSmall5.data('owlCarousel');
            slider.userItems.removeClass('active').eq(this.currentSlide).addClass('active');
            slider.goTo(this.currentSlide);
        }
    });
    var gallerSmall5 = $("#photo-gallery5 .photo-gallery__small");
    gallerSmall5.owlCarousel({
        items: 4,
        itemsDesktop: [920, 3],
        itemsDesktopSmall: [768, 5],
        itemsTablet: [768, 4],
        itemsMobile: [460, 2],
        afterInit: function () {
            this.userItems.eq(0).addClass('active');
            this.userItems.find('a').on('click', gallerBig5Update);
        }
    });
    function gallerBig5Update()
    {
        var slider = gallerBig5.data('owlCarousel');
        var current = $(this).parents('.owl-item').index();
        slider.goTo(current);
        return false;
    }

    $("#photo-gallery5 .next").click(function ()
    {
        gallerSmall5.trigger('owl.next');
    });
    $("#photo-gallery5 .prev").click(function ()
    {
        gallerSmall5.trigger('owl.prev');
    });

    $Player.init();
    mobi03 =
            {
                setUData: function (data, value)
                {
                    if (typeof localStorage.mobi03 === 'undefined')
                    {
                        localStorage.mobi03 = "true";
                    }
                    if (typeof data === 'object')
                    {
                        $.each(data, function (index, row)
                        {
                            localStorage.setItem("mobi03." + index, (typeof (row) === 'object') ? JSON.stringify(row) : row);
                        });
                    }
                    else if (typeof value !== 'undefined')
                    {
                        localStorage.setItem("mobi03." + data, value);
                    }

                },
                getUData: function (data)
                {
                    if (typeof localStorage.mobi03 === 'undefined')
                    {
                        return null;
                    }
                    if (typeof localStorage["mobi03." + data ] === 'undefined')
                    {
                        return null;
                    }
                    var _temp = false;
                    try
                    {
                        _temp = JSON.parse(localStorage.getItem("mobi03." + data));
                    }
                    catch (Exception)
                    {
                        _temp = localStorage.getItem("mobi03." + data);
                    }
                    return _temp;
                },
                showMessage: function (title, description)
                {
                    $.fancybox('<div class="buying-services" id="_NOTIFICATION_">\n\
                                            <h3 class="popup-title">' + title + '</h3>\n\
                                            <p class="global-warning">' + description + '</p>\n\
                                        </div>');
                    return false;
                },
                cyr2lat: function (str, firsturl)
                {
                    var cyr2latChars = new Array(
                            ['а', 'a'], ['б', 'b'], ['в', 'v'], ['г', 'g'], ['д', 'd'], ['е', 'e'], ['ё', 'yo'], ['ж', 'zh'], ['з', 'z'],
                            ['и', 'i'], ['й', 'y'], ['к', 'k'], ['л', 'l'], ['м', 'm'], ['н', 'n'], ['о', 'o'], ['п', 'p'], ['р', 'r'],
                            ['с', 's'], ['т', 't'], ['у', 'u'], ['ф', 'f'], ['х', 'h'], ['ц', 'c'], ['ч', 'ch'], ['ш', 'sh'], ['щ', 'shch'], ['ъ', ''], ['ы', 'y'], ['ь', ''], ['э', 'e'], ['ю', 'yu'], ['я', 'ya'],
                            ['А', 'A'], ['Б', 'B'], ['В', 'V'], ['Г', 'G'], ['Д', 'D'], ['Е', 'E'], ['Ё', 'YO'], ['Ж', 'ZH'], ['З', 'Z'],
                            ['�?', 'I'], ['Й', 'Y'], ['К', 'K'], ['Л', 'L'], ['М', 'M'], ['Н', 'N'], ['О', 'O'], ['П', 'P'], ['Р', 'R'],
                            ['С', 'S'], ['Т', 'T'], ['У', 'U'], ['Ф', 'F'], ['Х', 'H'], ['Ц', 'C'], ['Ч', 'CH'], ['Ш', 'SH'], ['Щ', 'SHCH'],
                            ['Ъ', ''], ['Ы', 'Y'], ['Ь', ''], ['Э', 'E'], ['Ю', 'YU'], ['Я', 'YA'], ['a', 'a'], ['b', 'b'], ['c', 'c'], ['d', 'd'], ['e', 'e'],
                            ['f', 'f'], ['g', 'g'], ['h', 'h'], ['i', 'i'], ['j', 'j'], ['k', 'k'], ['l', 'l'], ['m', 'm'], ['n', 'n'], ['o', 'o'],
                            ['p', 'p'], ['q', 'q'], ['r', 'r'], ['s', 's'], ['t', 't'], ['u', 'u'], ['v', 'v'], ['w', 'w'], ['x', 'x'], ['y', 'y'],
                            ['z', 'z'], ['A', 'A'], ['B', 'B'], ['C', 'C'], ['D', 'D'], ['E', 'E'], ['F', 'F'], ['G', 'G'], ['H', 'H'], ['I', 'I'], ['J', 'J'], ['K', 'K'],
                            ['L', 'L'], ['M', 'M'], ['N', 'N'], ['O', 'O'], ['P', 'P'], ['Q', 'Q'], ['R', 'R'], ['S', 'S'], ['T', 'T'], ['U', 'U'], ['V', 'V'], ['W', 'W'], ['X', 'X'], ['Y', 'Y'],
                            ['Z', 'Z'], [' ', '-'], ['0', '0'], ['1', '1'], ['2', '2'], ['3', '3'], ['4', '4'], ['5', '5'], ['6', '6'], ['7', '7'], ['8', '8'], ['9', '9'], ['-', '-']
                            );
                    var newStr = new String();
                    for (var i = 0; i < str.length; i++)
                    {
                        ch = str.charAt(i);
                        var newCh = '';
                        for (var j = 0; j < cyr2latChars.length; j++)
                            if (ch == cyr2latChars[j][0])
                                newCh = cyr2latChars[j][1];
                        newStr += newCh;
                    }
                    newStr = newStr.replace(/[-]{2,}/gim, '-').replace(/\n/gim, '').toLowerCase();
                    if (typeof firsturl != 'undefined' && firsturl != '')
                    {
                        newStr = firsturl + '/' + newStr;
                    }
                    return newStr;
                },
                sendGoogleStatic: function (category, anchor, label)
                {
                    _gaq.push(['_trackEvent', category, anchor, label]);
                }
            }

    /*- fancybox -*/
    $('.fancybox').fancybox({
        fitToView: false,
        helpers: {
            overlay: {
                locked: false
            }
        }
    });

    $("div.sliderkit-nav-clip>ul>li").on("click", function ()
    {
        $('div.sliderkit-nav-clip>ul>li').removeClass('sliderkit-selected');
        $(this).addClass('sliderkit-selected');
    });

    jQuery(".masked-phone").mask("+7 (999) 999-99-99");
    if (window.location.hash == "#en")
        $('#supply-en').trigger('click');
    
    $('iframe[name="google_conversion_frame"]').hide();
});

var hasCoockie = localStorage.getItem('battery');

var battery = $('.battery');
var batteryDescr = $('.battery-descr');
var batteryBtn = $('.battery-button');
var batteryEnabled = hasCoockie || !batteryBtn.hasClass('disabled');
var batteryDiscount = parseInt(hasCoockie) || 5;

function fillBattery() {
    var text = $('.discount-text li').eq(batteryDiscount - 5).html();

    batteryDescr
            .html(text);

    battery
            .removeClass()
            .addClass('battery __x15');

    setTimeout(function () {
        battery
                .removeClass('__x15')
                .addClass('__x' + batteryDiscount)
    }, 500)
}

function enableBattery() {
    batteryEnabled = true;
    batteryBtn.removeClass('disabled');
}

$('.battery-wrap__inner').on('click', '.battery-button', function () {
    var hasCoockie = localStorage.getItem('battery');
    _gaq.push(["_trackEvent", "skidka", "play", window.location.href], true);
    if (hasCoockie) {
        batteryDiscount = Math.floor(Math.random() * (15 - 5 + 1)) + 5;
        localStorage.setItem('battery', batteryDiscount);
        changetSale(batteryDiscount);
        fillBattery();
        return false;
    } else {
        mobi03.showMessage('Подтвердите действие на mobi03.ru', 'Для того чтобы воспользоваться генератором скидки нужно поделиться страницей с друзьями в одной из перечисленных ниже социальной сети');
        return false;
    }
});

if (batteryEnabled)
    enableBattery();
fillBattery();


function changetSale(sale) {
    $.get('/main/ajax/skidka', {skidka: sale});
    $('.application-form form fieldset p strong:nth-child(2)').text(sale + '%');
    $('.global-form form fieldset p strong:nth-child(2)').text(sale + '%');
    $('.disc').text(sale);
}

function like() {
    batteryDiscount = Math.floor(Math.random() * (15 - 5 + 1)) + 5;
    localStorage.setItem('battery', batteryDiscount);
    changetSale(batteryDiscount);
    fillBattery();
    enableBattery();
}
